# -*- coding: utf-8 -*-

__author__ = """WebLion <support@weblion.psu.edu>"""
__docformat__ = 'plaintext'

#
# Test-cases for class(es) Group
#

from Products.ContactsDirectory.config import *
from Products.ContactsDirectory.tests.testPlone import testPlone

class testGroup(testPlone):
    """Test-cases for class(es) Group."""

    def afterSetUp(self):
        self.loginAsPortalOwner()
        self.directory = self.getPopulatedDirectory()
        self.person = self.getPerson(id='abc123', firstName="Test", lastName="Person")
        self.sf = self.directory.getGroupsFolder()
        self.sf.invokeFactory(type_name="CDGroup", id="test-group")
        self.group = self.sf['test-group']

    def testAddPersonToGroup(self):
        #Assign a person to a group
        self.group.addReference(self.person, relationship='GroupInformation')
        self.failUnless(self.person in self.group.getPeople())
        
    def testGetClassifications(self):
        # There aren't any people associated with this classification. We should get a [] back.
        classifications = self.group.getClassifications()
        self.failUnless(classifications == [])
        # Add a person, we should now get the full list
        self.group.addReference(self.person, 'GroupInformation')
        # And it should be full of brains. Braaaaains.
        for cls in self.group.getClassifications():
            self.failUnless(cls.getObject())
        self.failUnless([brain.getObject() for brain in self.group.getClassifications()] == [brain.getObject() for brain in self.directory.getClassifications()])
        
    def testAddSubGroup(self):
        #Make sure we can add a group within a group
        self.group.invokeFactory(type_name="CDGroup", id="test-subgroup")
        self.failUnless('test-subgroup' in self.group.contentIds())
    
    def testFTISetup(self):
        """ Make sure the FTI is pulling info from the GS types profile """
        self.failUnless(self.portal.portal_types['CDGroup'].Title() != "AT Content Type")
        self.failUnless(self.portal.portal_types['CDGroupInformation'].Title() != "AT Content Type")

    def testObjectReorder(self):
        """ Make sure we can reorder objects within this folderish content type. """
        self.group.invokeFactory(type_name="CDGroup", id="o1") 
        self.group.invokeFactory(type_name="CDGroup", id="o2") 
        self.group.invokeFactory(type_name="CDGroup", id="o3") 
        self.group.moveObjectsByDelta(['o3'], -100)
        self.failUnless(self.group.getObjectPosition('o3') == 0, "CDGroup Subobject 'o3' should be at position 0.")


def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(testGroup))
    return suite
